<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016 杭州博也网络科技, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace app\shop\api;

use app\common\api\BaseApi;
use app\shop\model\OrdersItem;
use app\shop\model\Orders;

class OrdersItemApi extends BaseApi
{

    /**
     * 统计求和
     */
    const SUM = "shop/OrdersItemApi/sum";

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "shop/OrdersItemApi/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "shop/OrdersItemApi/add";
    /**
     * 保存
     */
    const SAVE = "shop/OrdersItemApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "shop/OrdersItemApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "shop/OrdersItemApi/delete";

    /**
     * 查询
     */
    const QUERY = "shop/OrdersItemApi/query";

    /**
     * 查询一条数据
     */
    const GET_INFO = "shop/OrdersItemApi/getInfo";

    /**
     * 统计单个商品的月销量
     */
    const MONTHLY_SALES = "shop/OrdersItemApi/monthlySales";

    /**
     * 统计单个商品的总销量
     */
    const TOTAL_SALES = "shop/OrdersItemApi/totalSales";
    const CHECK_ORDER_ITEM = "shop/OrdersItemApi/checkOrderItem";



    protected function _init()
    {
        $this->model = new OrdersItem();
    }

    public function checkOrderItem($order_code,$pids,$psids,$gids,$packids){
        if(!is_array($pids))    return $this->apiReturnErr('参数错误:pids');
        if(!is_array($psids))   return $this->apiReturnErr('参数错误:psks');
        if(!is_array($gids))    return $this->apiReturnErr('参数错误:gros');
        if(!is_array($packids)) return $this->apiReturnErr('参数错误:pcks');
        $len = count($pids);
        if($len!=count($psids) || $len!=count($gids) || $len!=count($packids)){
            return $this->apiReturnErr('参数不匹配');
        }
        //查询订单详情
        $r = $this->queryNoPaging(['order_code'=>$order_code]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(empty($r['info'])) return $this->apiReturnErr('参数错误:order_code');
        $o_ids = [];
        foreach ($r['info'] as $v) {
            $o_ids[] = $v['p_id'].'-'.$v['psku_id'].'-'.$v['group_id'].'-'.$v['package_id'];
        }
        //比较
        foreach ($pids as $k=>$v) {
            // in_array($v.'-'.$psids[$k].'-'.$gids[$k].'-'.$packids[$k],$o_ids);
            $key = array_search($v.'-'.$psids[$k].'-'.$gids[$k].'-'.$packids[$k],$o_ids);
            if(false === $key) return $this->apiReturnErr('orderItem错误');
            unset($o_ids[$key]);
        }
        if(!empty($o_ids)) return $this->aiReturnErr('订单商品不完整');
        return $this->apiReturnSuc('pass');
    }
    // public function queryNoPaging($map = null, $order = false, $fields = false){
    //     $query = $this->model;
    //     if(!empty($map))      $query = $query->where($map);
    //     if(false !== $order)  $query = $query->order($order);
    //     if(false !== $fields) $query = $query->field($fields);
    //     $list = $query -> select();

    //     if (false === $list) {
    //         $error = $this -> model -> getDbError();
    //         return $this -> apiReturnErr($error);
    //     }

    //     return $this -> apiReturnSuc($list);
    // }
    /**
     * 统计单个商品的总销量
     */
    public function totalSales($p_id)
    {
        //TODO: 当前计算不管是否成交，都计入销量
        $map['p_id'] = $p_id;

        $result = $this->model->where($map)->count();

        if ($result === false) {
            return $this->apiReturnErr($this->model->getDbError());
        }

        if (is_null($result)) {
            //空无任何销量
            return $this->apiReturnSuc(0);
        }


        return $this->apiReturnSuc($result);

    }


    /**
     * 统计单个商品的月销量
     */
    public function monthlySales($p_id)
    {
//        //TODO: 当前计算不管是否成交，都计入销量
//        $map['p_id'] = $p_id;
//        $currentTime = time();
//        //30天
//        $map['createtime'] = array(array('gt', $currentTime - 30 * 3600 * 24), array('lt', $currentTime));
//
//        $result = $this->model->where($map)->select();
//
//        if ($result === false) {
//            return $this->apiReturnErr($this->model->getDbError());
//        }
//        if (is_null($result)) {
//            //空无任何销量
//            return $this->apiReturnSuc(0);
//        }
//        $orders_code_arr = array();
//
//        foreach ($result as $vo) {
//            array_push($orders_code_arr, $vo['order_code']);
//        }
//        if (count($orders_code_arr) == 0) {
//            return $this->apiReturnSuc(0);
//        }
//
//        $model = new OrdersModel();
//        $mapOrders = array();
//
//        //TODO: in 语句， 性能问题，需要修正
//        $mapOrders['order_code'] = array('in', $orders_code_arr);
//        // 已支付
//        $mapOrders['pay_status'] = OrdersModel::ORDER_PAID;
//
//        $result = $model->where(array($mapOrders))->select();
//
//        if ($result === false) {
//            return $this->apiReturnErr($this->model->getDbError());
//        }
//
//        $cnt = 0;
//        foreach ($result as $vo) {
//            if(in_array($vo['order_code'],$orders_code_arr)){
//                $cnt++;
//            }
//        }

        return $this->apiReturnSuc(0);

    }


}
